package com.cheyun.netsalev3.view.aftersale

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.aftersale.CarOwnerData
import com.cheyun.netsalev3.databinding.ActivityChooseCarOwnerBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.aftersale.ChooseCarOwnerActivityViewModel

class ChooseCarOwnerActivity : BaseActivity<ActivityChooseCarOwnerBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_choose_car_owner
    }

    override fun initViewModel(): ViewModel? {
        val factory = InjectorUtils.getChooseCarOwnerActivityViewModelFactory()
        return ViewModelProviders.of(this, factory).get(ChooseCarOwnerActivityViewModel::class.java)
    }

    private lateinit var adapter: MyPagedListAdapter<CarOwnerData>

    override fun initView() {
        adapter = MyPagedListAdapter(R.layout.choose_car_owner_item, binding.viewModel)

        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }
        var hashMap :HashMap<String, String> = HashMap()
        var source = intent.getStringExtra("source")
        var nkey = intent.getStringExtra("nkey")

        var license = ""
        var engineno = ""
        var vin = ""
        if(intent.hasExtra("license")) {
            license = intent.getStringExtra("license")
        }
        if(intent.hasExtra("engineno")) {
            engineno = intent.getStringExtra("engineno")
        }
        if(intent.hasExtra("vin")) {
            vin = intent.getStringExtra("vin")
        }
        hashMap.put("source",source)
        hashMap.put("nkey",nkey)
        hashMap.put("license",license)
        hashMap.put("engineno",engineno)
        hashMap.put("vin",vin)
        binding.viewModel?.initData(hashMap)
    }

    override fun initData() {
        binding.viewModel?.listdata?.observe(this, Observer { list ->
            adapter.submitList(list)
        })

        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing=it== NetworkState.LOADING
            adapter.setNetworkState(it)
        })
    }


}
